package book

import (
	"fmt"

	appErrors "BookStack/pkg/errors"
)

// ========================== 书籍相关错误 ==========================

// BookNotFound 书籍不存在错误
func BookNotFound(id uint) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeBookNotFound,
		Message: fmt.Sprintf("书籍不存在: %d", id),
	}
}

// BookAlreadyExists 书籍已存在错误
func BookAlreadyExists(title string) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeBookAlreadyExists,
		Message: fmt.Sprintf("书籍已存在: %s", title),
	}
}

// BookSlugExists Slug已存在错误
func BookSlugExists(slug string) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeBookSlugExists,
		Message: fmt.Sprintf("书籍Slug已存在: %s", slug),
	}
}

// BookTitleRequired 书籍标题必填错误
func BookTitleRequired() error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeBookTitleRequired,
		Message: "书籍标题不能为空",
	}
}

// BookAuthorRequired 作者必填错误
func BookAuthorRequired() error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeBookAuthorRequired,
		Message: "作者不能为空",
	}
}

// BookSlugRequired 书籍标识必填错误
func BookSlugRequired() error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeBookSlugRequired,
		Message: "书籍标识不能为空",
	}
}

// BookCategoryRequired 书籍分类必填错误
func BookCategoryRequired() error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeBookCategoryRequired,
		Message: "书籍分类不能为空",
	}
}

// BookStatusInvalid 书籍状态无效错误
func BookStatusInvalid(status int) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeBookStatusInvalid,
		Message: fmt.Sprintf("无效的书籍状态: %d", status),
	}
}

// BookRecommendStatusInvalid 书籍推荐状态无效错误
func BookRecommendStatusInvalid(recommended bool) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeBookRecommendStatusInvalid,
		Message: fmt.Sprintf("无效的推荐状态: %v", recommended),
	}
}

// BookNotInRecycleBin 书籍不在回收站中错误
func BookNotInRecycleBin(id uint) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeBookNotInRecycleBin,
		Message: fmt.Sprintf("书籍不在回收站中: %d", id),
	}
}

// BookInRecycleBin 书籍在回收站中错误
func BookInRecycleBin(id uint) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeBookInRecycleBin,
		Message: fmt.Sprintf("书籍在回收站中: %d", id),
	}
}

// ========================== 章节相关错误 ==========================

// ChapterNotFound 章节不存在错误
func ChapterNotFound(id uint) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeChapterNotFound,
		Message: fmt.Sprintf("章节不存在: %d", id),
	}
}

// ChapterAlreadyExists 章节已存在错误
func ChapterAlreadyExists(title string, bookID uint) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeChapterAlreadyExists,
		Message: fmt.Sprintf("章节已存在: %s, 书籍ID: %d", title, bookID),
	}
}

// ChapterSlugExists 章节Slug已存在错误
func ChapterSlugExists(slug string) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeChapterSlugExists,
		Message: fmt.Sprintf("章节Slug已存在: %s", slug),
	}
}

// ChapterTitleRequired 章节标题必填错误
func ChapterTitleRequired() error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeChapterTitleRequired,
		Message: "章节标题不能为空",
	}
}

// ChapterContentRequired 章节内容必填错误
func ChapterContentRequired() error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeChapterContentRequired,
		Message: "章节内容不能为空",
	}
}

// ChapterBookIDRequired 章节书籍ID必填错误
func ChapterBookIDRequired() error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeChapterBookIDRequired,
		Message: "章节所属书籍ID不能为空",
	}
}

// ChapterNotInRecycleBin 章节不在回收站中错误
func ChapterNotInRecycleBin(id uint) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeChapterNotInRecycleBin,
		Message: fmt.Sprintf("章节不在回收站中: %d", id),
	}
}

// ChapterNotBelongToBook 章节不属于该书籍错误
func ChapterNotBelongToBook(chapterIDs []uint, bookID uint) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeChapterNotBelongToBook,
		Message: fmt.Sprintf("部分章节不属于书籍 %d", bookID),
	}
}

// ========================== 回收站相关错误 ==========================

// RecycleBinItemNotFound 回收站项目不存在错误
func RecycleBinItemNotFound(id uint) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeRecycleBinItemNotFound,
		Message: fmt.Sprintf("回收站项目不存在: %d", id),
	}
}

// RecycleBinEmpty 回收站为空错误
func RecycleBinEmpty() error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeRecycleBinEmpty,
		Message: "回收站为空",
	}
}

// InvalidRecycleBinEntityType 无效的回收站实体类型错误
func InvalidRecycleBinEntityType(entityType string) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeInvalidRecycleBinEntityType,
		Message: fmt.Sprintf("无效的回收站实体类型: %s", entityType),
	}
}

// ========================== 通用错误 ==========================

// DatabaseError 数据库错误
func DatabaseError(err error) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeDatabase,
		Message: err.Error(),
	}
}

// InvalidRequestError 无效请求错误
func InvalidRequestError(message string) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeBadRequest,
		Message: message,
	}
}

// BatchOperationError 批量操作错误
func BatchOperationError(message string) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeBatchOperationFailed,
		Message: message,
	}
}

// ValidationError 数据验证错误
func ValidationError(field, message string) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeValidation,
		Message: fmt.Sprintf("字段 %s: %s", field, message),
	}
}

// UnauthorizedError 未授权错误
func UnauthorizedError() error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeUnauthorized,
		Message: "未授权访问",
	}
}

// ForbiddenError 禁止访问错误
func ForbiddenError() error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeForbidden,
		Message: "无权限执行此操作",
	}
}

// InternalError 内部服务器错误
func InternalError(err error) error {
	return &appErrors.AppError{
		Code:    appErrors.ErrCodeInternal,
		Message: err.Error(),
	}
}
